package com.fp.m7.manager;

import java.io.Serializable;
import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

import com.fp.m7.api.manager.AccountManager;
import com.fp.m7.api.model.Account;
import com.fp.m7.dao.AccountDao;
import com.fp.m7.dao.CommentDao;

public class AccountManagerImpl implements AccountManager {

	@Autowired
	private AccountDao accountDao;

	@Autowired
	private CommentDao commentDao;

	@Override
	public Account findById(Serializable id) {
		return accountDao.findById((ObjectId) id);
	}

	@Override
	public Account update(Account account) {
		return accountDao.update((com.fp.m7.model.Account) account);
	}

	@Override
	public void delete(Account account) {
		accountDao.delete((com.fp.m7.model.Account) account);
		commentDao.removeUsersComments((com.fp.m7.model.Account) account);
	}

	@Override
	public Account createNew(String username, String password, String nickname) {
		Account account = new com.fp.m7.model.Account();
		account.setUsername(username);
		account.setPassword(password);
		account.setNickname(nickname);
		return accountDao.save((com.fp.m7.model.Account) account);
	}

	@Override
	public Account findByUsername(String username) {
		return accountDao.findByUsername(username);
	}

	@Override
	public Account findByNickname(String nickname) {
		return accountDao.findByNickname(nickname);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Account> findRandom(int count) {
		return (List) accountDao.findRandom(count);
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
}
